<template>
    <Modal
        title="jira登录"
        :visible="visible"
        okText="登录"
        :ok-button-props="{ loading: loading }"
        @ok="handleSubmit"
        @cancel="visible = false"
        v-if="visible"
    >
        <Form layout="inline" ref="ruleFormRef" :model="form" :rules="rules" @keypress.enter="handleSubmit">
            <Row>
                <Col :span="20">
                    <FormItem label="jira帐号" required has-feedback name="account">
                        <Input v-model:value="form.account" autocomplete="off" />
                    </FormItem>
                </Col>
                <Col :span="20">
                    <FormItem label="密码" required has-feedback name="password">
                        <Input v-model:value="form.password" type="password" autocomplete="off" />
                    </FormItem>
                </Col>
            </Row>
        </Form>
    </Modal>
</template>
<script lang="ts">
import jse from '@/utils/rsa';
import api from '@/api';
import store from '@/store';
import { Modal, Form, Row, Col, Input } from 'ant-design-vue';
import { notification } from 'ant-design-vue';
import { defineComponent, ref, unref } from 'vue';
interface FormState {
    account: string;
    password: string;
}
export default defineComponent({
    name: 'JiraLoginModal',
    components: {
        Form,
        FormItem: Form.Item,
        Modal,
        Row,
        Col,
        Input
    },
    props: {
        // 系统
        systemId: {
            type: [String, Number],
            default: ''
        },
        // jira设置中的url
        url: {
            type: String,
            default: ''
        },
        onOk: {
            type: Function
        }
    },
    setup(props) {
        const loading = ref(false);
        const visible = ref(false);
        const ruleFormRef = ref();
        const form = ref<FormState>({
            account: '',
            password: ''
        });
        const rules = {
            account: [{ required: true, message: '用户名不能为空', trigger: 'change' }],
            password: [{ required: true, message: '密码不能为空', trigger: 'change' }]
        };
        const handleSubmit = async () => {
            const res = await ruleFormRef.value.validate();
            if (res) {
                loading.value = true;
                const pwd = jse.encrypt(unref(form).password);
                const { empId } = store.getters.userInfo.employeeDTO;
                const query = {
                    acct: unref(form).account,
                    pwd,
                    userId: empId
                };
                const { success, errorMsg } = await api.jiraAccountModel.save({
                    ...query
                });
                loading.value = false;
                if (success) {
                    visible.value = false;
                    // emit('success');
                    props.onOk && props.onOk();
                } else {
                    notification.error({
                        message: '登陆失败',
                        description: errorMsg
                    });
                }
            }
        };
        const open = () => {
            visible.value = true;
            form.value = {
                account: '',
                password: ''
            };
        };
        return {
            form,
            ruleFormRef,
            visible,
            rules,
            handleSubmit,
            open,
            loading
        };
    }
});
</script>
